<?php
/**
 * Bootstrap.php
 *
 * 启动初始化脚本
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://momoger.com/license
 *
 * @category   Zend
 * @package    Zend_Application
 * @author 		Guya
 * @version    	1.0
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */

/** Zend_Application_Bootstrap_Bootstrap */
//require_once 'Zend/Application/Bootstrap/Bootstrap.php';

/**
 * 应用项目启动器类
 *
 * @category   Zend
 * @package    Zend_Application
 * @subpackage Bootstrap
 * @copyright  Copyright © 2009 www.momoger.com All Rights Reserved.
 */
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    /**
     * 初始化系统 SESSION 存储设置
     *
     * 使用数据库存储 用户 session
     */
    protected function _initSession() {
    	//get system config information
    	$_config = $this->getOptions();

    	if (isset($_config['sess']['enabled']) && $_config['sess']['enabled']) {
	    	//get session config
	    	if (isset($_config['sess']['db']['enabled']) && $_config['sess']['db']['enabled']) {
	    		$dbAdapter = Zend_Db::factory($_config['sess']['db']['adapter'], $_config['sess']['db']['params']);
	    		$__sessConfig = array(
	    			'db' => $dbAdapter,
					'name' => 'zf_sessions',
					'primary' => 'sessID',
					'modifiedColumn' => 'modified',
					'lifetimeColumn' => 'lifetime',
					'dataColumn' => 'sessData'
				);
				Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($__sessConfig));
	    	}

			//open session
			Zend_Session::start();
    	}
    }

	/**
     * 初始化系统缓存设置.
     *
     * 系统缓存设置， 使用文件保存缓存数据.
     *
     */
	protected function _initCache() {
        $_config = $this->getOptions();
        if (isset($_config['cache']['enabled']) && $_config['cache']['enabled']) {
        	$cache = Zend_Cache::factory('Core', 'File', $_config['cache']['frontend'], $_config['cache']['backend']);
        	Zend_Registry::set('cache',$cache);
        }
    }

    /**
     * 初始化数据库连接配置.
     *
     * 可以使用多个数据库配置， 在这里进行初始化， 也可以在用的时候再初始化.
     */
    protected function _initDb() {
    	$config = $this->getOptions();
    	$dbAdapter = Zend_Db::factory($config['db']['adapter'], $config['db']['params']);
    	Zend_Db_Table::setDefaultAdapter($dbAdapter);
    	Zend_Registry::set('defaultDbAdapter', $dbAdapter);
    }
}